package yxy.game.pm2.command.union.contrib;

import yxy.cherry.data.DataCenter;
import yxy.cherry.data.bean.unionstagereward;
import yxy.cherry.game.base.message.MessageEmitterHandler;
import yxy.cherry.game.base.message.annotation.MessageSource;
import yxy.cherry.game.base.message.annotation.MessageType;
import yxy.game.pm2.Result;
import yxy.game.pm2.bean.Currency;
import yxy.game.pm2.bean.module.RedisLock;
import yxy.game.pm2.bean.mq.CCReason;
import yxy.game.pm2.command.Command;
import yxy.game.pm2.message.UnionMessage;
import yxy.game.pm2.module.CurrencyHandle;
import yxy.game.pm2.module.union.UnionBean;
import yxy.game.pm2.module.union.UnionContribution;
import yxy.game.pm2.module.union.UnionHandle;
import yxy.game.pm2.module.union.UnionHandle.UnionPlayerEntry;
import yxy.game.pm2.module.union.player.UnionPlayer;
import yxy.game.pm2.module.union.player.UnionPlayerContribution;

import java.util.Collection;

/** 联盟捐献 联盟捐献领取奖励 */
@MessageType(proto = Command.UnionContributeBonus, classtype = UnionMessage.UnionContributeBonusRequest.class)
public class CMD_UnionContributeBonus extends Command<UnionMessage.UnionContributeBonusRequest> {

	@Override
	public void execute(MessageSource messageSource, UnionMessage.UnionContributeBonusRequest request) {
		String playerCode = messageSource.getTag();
		int box = request.getBox();

		UnionMessage.UnionContributeBonusResponse.Builder response = UnionMessage.UnionContributeBonusResponse.newBuilder();

		unionstagereward data = DataCenter.getData(box, unionstagereward.class);
		if (data == null) {
			send(messageSource, response, Result.ParamError);
			return;
		}
		// 检查是否已经有联盟
		UnionPlayerEntry entry = new UnionPlayerEntry(playerCode);
		Result result = new UnionHandle().checkPlayerUnion(entry);
		if (!result.equals(Result.UnionIn)) {
			send(messageSource, response, result);
			return;
		}
		RedisLock uLock = UnionBean.lock(entry.getUnionBean().getCode());
		RedisLock pLock = UnionPlayer.lock(entry.getPlayerCode());
		try {
			if (uLock.tryLock() && pLock.tryLock()) {
				UnionBean union = entry.reloadUnionBean();
				UnionPlayer unionPlayer = entry.reloadUnionPlayer();

				UnionPlayerContribution playerContribution = unionPlayer.getContribution();

				UnionContribution contribution = UnionContribution.get(union);

				if (contribution.getProgress() < data.getStagecondition()) {
					send(messageSource, response, Result.UnionContribBonusLowScore);// 捐献积分还不够
					return;
				}

				if (playerContribution.getBox().contains(data.getCode())) {
					send(messageSource, response, Result.UnionContribBonusRewarded);// 你已经领过这个奖励
					return;
				}

				Collection<Currency> rewards = Currency.create(data.getRewardtype(), data.getRewardid(), data.getRewardnum());
				new CurrencyHandle().addCurrency(playerCode, CCReason.联盟捐献, rewards);

				playerContribution.getBox().add(data.getCode());
				unionPlayer.set();

				response.addAllRewards(new CurrencyHandle().packItems(rewards));
				send(messageSource, response, Result.Success);
			}
		} catch (Exception e) {
			e.printStackTrace();
			send(messageSource, response, Result.DataError);
		} finally {
			uLock.unlock();
			pLock.unlock();
		}
	}

	private void send(MessageSource messageSource, UnionMessage.UnionContributeBonusResponse.Builder response, Result result) {
		response.setResult(result.packResult());
		MessageEmitterHandler.getInstance().send(messageSource, response.build());
	}
}